package org.simon.myauctioneer;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.simon.myauctioneer.dto.User;
import org.simon.myauctioneer.service.UserService;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet({ "/register", "/login", "/logout" })
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession httpSession = request.getSession();
		httpSession.invalidate();
		request.setAttribute("title", "Logout successfull");
		request.setAttribute("content", "The Logout was successfull.");
		RequestDispatcher dispatcher = request.getRequestDispatcher("success.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();
		
		if (servletPath.equals("/register")) {
			String name, address, phone, email, password;
			
			name = request.getParameter("name");
			address = request.getParameter("address");
			phone = request.getParameter("phone");
			email = request.getParameter("email");
			password = request.getParameter("password");
			
			if ((name == null || name.trim() == "")
					|| (address == null || address.trim() == "")
					|| (phone == null || phone.trim() == "")
					|| (email == null || email.trim() == "")
					|| (password == null || password.trim() == "")) {
				response.sendRedirect("register.jsp");
			} else {
				UserService userService = new UserService();
				boolean result = userService.register(name, address, phone, email, password);
				
				if (result) {
					request.setAttribute("title", "Registration successfull");
					request.setAttribute("content", "The registration was successfull.");
					RequestDispatcher dispatcher = request.getRequestDispatcher("success.jsp");
					dispatcher.forward(request, response);
				} else {
					response.sendRedirect("register.jsp");
				}
			}
		} else if (servletPath.equals("/login")) {
			String email, password;
			
			email = request.getParameter("email");
			password = request.getParameter("password");
			
			UserService userService = new UserService();
			boolean result = userService.authenticate(email, password);
			
			if (result) {
				User user = userService.getUserDetails(email);
				HttpSession httpSession = request.getSession();
				httpSession.setAttribute("user", user);
				request.setAttribute("title", "Login successfull");
				request.setAttribute("content", "The login was successfull.");
				RequestDispatcher dispatcher = request.getRequestDispatcher("success.jsp");
				dispatcher.forward(request, response);
			} else {
				response.sendRedirect("index.jsp");
			}
		}
	}

}
